import Request from 'utils/request';

export default class PermissionApi {
  static getRoleList() {
    const apiName = 'api/role/all';
    return Request.post(apiName).then(res => {
      return res.data;
    });
  }

  static getBranchList() {
    const apiName = 'api/branch/all';
    return Request.post(apiName, {
      status: 1
    }).then(res => {
      let branchList = res.data;
      branchList = _.map(branchList, branch => {
        return {
          branchId: branch.branchId,
          branchName: branch.branchName
        };
      });
      return branchList;
    });
  }

  static getPartners(id) {
    const apiName = 'api/partner/getPartnersByBrandhId';
    return Request.post(apiName, {
      status: 1,
      branchId: id ? id : ''
    }).then(res => {
      let partnerList = res.data;
      partnerList = _.map(partnerList, partner => {
        return {
          id: partner.partnerId,
          name: partner.partnerName
        };
      });
      return partnerList;
    });
  }

  static getAllUser(search, pagination) {
    const apiName = 'api/viewuser/list';
    let postData = search.getObject(pagination);
    return Request.post(apiName, postData).then(res => {
      pagination.totalCount = res.rows;
      return res.data;
    });
  }

  static addUserInfo(userForm) {
    const apiName = 'api/viewuser/save';
    let postData = userForm.getObject();
    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static updateUserInfo(userForm) {
    const apiName = 'api/viewuser/update';
    let postData = userForm.getObject();
    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static getAllRoleList(search, pagination) {
    const apiName = 'api/role/list';
    let postData = search.getObject(pagination);
    return Request.post(apiName, postData).then(res => {
      pagination.totalCount = res.rows;
      return res.data;
    });
  }

  static addRole(form) {
    const apiName = 'api/role/save';
    return Request.post(apiName, form).then(res => {
      return res.msg;
    });
  }

  static updateRole(form) {
    const apiName = 'api/role/update';
    return Request.post(apiName, form).then(res => {
      return res.msg;
    });
  }

  static delRole(ids) {
    const apiName = 'api/role/deleteIds';
    return Request.post(apiName, ids).then(res => {
      return res.msg;
    });
  }

  static getPermissionApiList(roleId) {
    const apiName = 'api/viewpermission/treeApiAll';
    return Request.post(apiName, {
      roleId
    }).then(res => {
      return res.data;
    });
  }

  static savePermissionApiData(form) {
    const apiName = 'api/viewpermission/apiPermissionSave';
    return Request.post(apiName, form).then(res => {
      return res.msg;
    });
  }

  static getMenuList(roleId) {
    const apiName = 'api/viewpermission/treeMenuAll';
    return Request.post(apiName, {
      roleId
    }).then(res => {
      return res.data;
    });
  }

  static saveMenuData(form) {
    const apiName = 'api/viewpermission/menuPermissionSave';
    return Request.post(apiName, form).then(res => {
      return res.msg;
    });
  }

  static getParentPermissions() {
    const apiName = 'api/viewpermission/all';
    return Request.post(apiName, {
      permissionType: 1
    }).then(res => {
      return res.data;
    });
  }

  static getAllPermission(search, pagination) {
    const apiName = 'api/viewpermission/list';
    let postData = search.getObject(pagination);
    return Request.post(apiName, postData).then(res => {
      pagination.totalCount = res.rows;
      return res.data;
    });
  }

  static addPermission(form) {
    const apiName = 'api/permission/save';
    let permissionType = form.permissionType;
    let postData = _.cloneDeep(form);
    if (permissionType === 1) {
      if (!form.permissionParent) {
        postData.permissionParent = 0;
      }
    } else if (permissionType === 2) {
      postData.permissionParent = 0;
    }

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static updatePermission(form) {
    const apiName = 'api/permission/update';
    let permissionType = form.permissionType;
    let postData = _.cloneDeep(form);
    if (permissionType === 1) {
      if (!form.permissionParent) {
        postData.permissionParent = 0;
      }
    } else if (permissionType === 2) {
      postData.permissionParent = 0;
    }

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static delPermission(ids) {
    const apiName = 'api/permission/deleteIds';
    return Request.post(apiName, ids).then(res => {
      return res.msg;
    });
  }
}
